package com.yxy.harmonyostest.slice;

import com.yxy.harmonyostest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

public class MainAbilityASlice extends AbilitySlice {
    private Button button;
    private Button buttonResult;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_ability);
        button = (Button) findComponentById(ResourceTable.Id_button2);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                /**
                 * 当发起导航的 AbilitySlice 和导航目标的 AbilitySlice
                 * 处于同一个 Page 时，您可以通过 present() 方法实现导航
                 */
                present(new MainAbilityAaSlice(), new Intent());
            }
        });
        buttonResult = (Button) findComponentById(ResourceTable.Id_button3);
        buttonResult.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                /**
                 * 如果开发者希望在用户从导航目标 AbilitySlice 返回时，能够获得其返回结果，则应当使用 presentForResult() 实现导航。
                 * 用户从导航目标 AbilitySlice 返回时，系统将回调 onResult() 来接收和处理返回结果，开发者需要重写该方法。
                 * 返回结果由导航目标 AbilitySlice 在其生命周期内通过 setResult() 进行设置。
                 */
                presentForResult(new MainAbilityAaSlice(), new Intent(), 888);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 带结果返回 回调
     * @param requestCode
     * @param resultIntent
     */
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        if (requestCode == 888) {
            System.out.println("返回");
        }
    }
}
